package com.wuxianedu.sqliteopenhelperdemo.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.wuxianedu.sqliteopenhelperdemo.Person;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by TED on 2016/9/19.
 */
public class DBHelper {

    private  MyDBHelper myDBHelper;
    private  SQLiteDatabase db;

    public DBHelper(Context context) {
        myDBHelper = new MyDBHelper(context);
        db = myDBHelper.getReadableDatabase();
        myDBHelper.getWritableDatabase();

    }

    /**
     * 增加数据
     *
     * @param values
     */
    public long insertData(ContentValues values) {
        return db.insert("person", null, values);
    }

    /**
     * 删除数据
     */
    public int deleteDataById(String... args) {
        // delete person where _id =  id;
        return db.delete("person", "_id = ?, name = ?", args);

    }

    /**
     * 更新
     *
     * @param values
     * @param args
     * @return
     */
    public int updataById(ContentValues values, String... args) {
        return db.update("person", values, "id = ?", args);
    }


    public List<Person> queryData() {
        //select _id, name, age, love from person
        Cursor cursor = db.query("person", null, null, null, null, null, null);
        List<Person> list = new ArrayList<>();
        while (cursor.moveToNext()){
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String age = cursor.getString(cursor.getColumnIndex("age"));
            String love = cursor.getString(cursor.getColumnIndex("love"));

            Person person = new Person();
            person.setName(name);
            person.setAge(age);
            person.setLove(love);
            list.add(person);
        }
        return  list;
    }

    public void close(){
        if (db!=null&&db.isOpen()){
            db.close();
            db = null;
        }

    }



    public void fun(){
//      插入                           表名    可为空的列，可以为null    一行的数据
        //public long insert (String table, String nullColumnHack, ContentValues values)


//       删除           DELETE FROM table WHERE id = 10  AND name = "xiaoye";
        //public int delete(String table,  //表名
        // .String whereClause, //where子句( id = ?  AND name = ?")
        // String[] whereArgs) // 对应where子句中的占位符 new String[]{"10", "xiaoye"}

        //更新  UPDATE table SET （列名1 = 值1， 列名2 = 值2） WHERE id = 10
        //public int update(String table,
//        ContentValues values,  //列名1 = 值1， 列名2 = 值2
//        String whereClause,
//        String[] whereArgs)


        //查询 SELECT (列名) FROM table where （子句） group by ( 子句)  order by ( ...) having (子句)
        //public Cursor query (boolean distinct, // 是否去重
        // String table, //表名
        // String[] columns,  //列名
        // String selection,  // where 子句
        // String[] selectionArgs,
        // String groupBy, //分组的子句（列名）
        // String having, // 条件 having avg(age)
        // String orderBy,// 排序
        // String limit,  //分页
        // CancellationSignal cancellationSignal)// 如果查询操作被取消，则会触发监听cancellationSignal.setoncancellListener

    }
}
